package com.ruoyi.qiankuantongji.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.cangku.domain.Cangku;
import com.ruoyi.cangku.mapper.CangkuMapper;
import com.ruoyi.qiankuantongji.domain.chaxunRuselt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.qiankuantongji.mapper.QiankuantongjiMapper;
import com.ruoyi.qiankuantongji.domain.Qiankuantongji;
import com.ruoyi.qiankuantongji.service.IQiankuantongjiService;
import com.ruoyi.common.core.text.Convert;

/**
 * 欠款统计Service业务层处理
 * 
 * @author ruoyi
 * @date 2023-02-23
 */
@Service
public class QiankuantongjiServiceImpl implements IQiankuantongjiService
{
    @Autowired
    private QiankuantongjiMapper qiankuantongjiMapper;

    @Autowired
    private CangkuMapper cangkuMapper;

    /**
     * 查询欠款统计
     * 
     * @param id 欠款统计主键
     * @return 欠款统计
     */
    @Override
    public Qiankuantongji selectQiankuantongjiById(Long id)
    {
        return qiankuantongjiMapper.selectQiankuantongjiById(id);
    }

    /**
     * 查询欠款统计列表
     * 
     * @param qiankuantongji 欠款统计
     * @return 欠款统计
     */
    @Override
    public List<Qiankuantongji> selectQiankuantongjiList(Qiankuantongji qiankuantongji)
    {
        return qiankuantongjiMapper.selectQiankuantongjiList(qiankuantongji);
    }

    @Override
    public List<Qiankuantongji> selectAll2QiankuanList(String cangku) {

        return qiankuantongjiMapper.selectAll2QiankuanList(cangku);
    }

    /**
     * 新增欠款统计
     * 
     * @param qiankuantongji 欠款统计
     * @return 结果
     */
    @Override
    public int insertQiankuantongji(Qiankuantongji qiankuantongji)
    {
        return qiankuantongjiMapper.insertQiankuantongji(qiankuantongji);
    }

    /**
     * 修改欠款统计
     * 
     * @param qiankuantongji 欠款统计
     * @return 结果
     */
    @Override
    public int updateQiankuantongji(Qiankuantongji qiankuantongji)
    {
        return qiankuantongjiMapper.updateQiankuantongji(qiankuantongji);
    }

    /**
     * 批量删除欠款统计
     * 
     * @param ids 需要删除的欠款统计主键
     * @return 结果
     */
    @Override
    public int deleteQiankuantongjiByIds(String ids)
    {
        return qiankuantongjiMapper.deleteQiankuantongjiByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除欠款统计信息
     * 
     * @param id 欠款统计主键
     * @return 结果
     */
    @Override
    public int deleteQiankuantongjiById(Long id)
    {
        return qiankuantongjiMapper.deleteQiankuantongjiById(id);
    }

    @Override
    public List<chaxunRuselt> selectAllQiankuantongjiList2All(chaxunRuselt chaxunRuselt){
        List<Cangku> cangkuList = cangkuMapper.selectCangkuAll();
        for (Cangku cangkui:cangkuList) {
            if (cangkui.getCangkuName().equals(chaxunRuselt.getCangku())) {
               return qiankuantongjiMapper.selectAllQiankuantongjiList2All(chaxunRuselt.getCangku(), chaxunRuselt.getKehuName());
            }
        }
        return qiankuantongjiMapper.selectAllQiankuantongjiList2All(null, chaxunRuselt.getKehuName());
    }

    @Override
    public List<chaxunRuselt> selectAllQiankuantongjiList(String cangku) {
        return qiankuantongjiMapper.selectAllQiankuantongjiList(cangku);
    }

    @Override
    public List<chaxunRuselt> selectAllQiankuantongjiList2month(String today) {
        String[] date_ = today.split("-");
        String firstday = null,lastday = null;
        switch (Integer.parseInt(date_[1])){
            case 10:
                firstday=date_[0]+"-10-1";lastday=date_[0]+"-10-31";
                break;
            case 8:
                firstday=date_[0]+"-8-1";lastday=date_[0]+"-8-31";
                break;
            case 7:
                firstday=date_[0]+"-7-1";lastday=date_[0]+"-7-31";
                break;
            case 5:
                firstday=date_[0]+"-5-1";lastday=date_[0]+"-5-31";
                break;
            case 3:
                firstday=date_[0]+"-3-1";lastday=date_[0]+"-3-31";
                break;
            case 1:
                firstday=date_[0]+"-1-1";lastday=date_[0]+"-1-31";
                break;
            case 12:
                firstday=date_[0]+"-12-1";lastday=date_[0]+"-12-31";
                break;

            case 4:
                firstday=date_[0]+"-4-1";lastday=date_[0]+"-4-30";
                break;
            case 6:
                firstday=date_[0]+"-6-1";lastday=date_[0]+"-6-30";
                break;
            case 9:
                firstday=date_[0]+"-9-1";lastday=date_[0]+"-9-30";
                break;
            case 11:
                firstday=date_[0]+"-11-1";lastday=date_[0]+"-11-30";
                break;

            case 2:
                firstday=date_[0]+"-2-1";lastday=date_[0]+"-2-29";
                break;
            default:break;
        }
        return qiankuantongjiMapper.selectAllQiankuantongjiList2month(firstday, lastday);
    }
}
